<?php
class multipage
{
    function __construct($num_items, $start_item, $add_prevnext_text = false,$per_page = false)
    {
        $total_pages = ceil($num_items/$per_page);
        if ( $total_pages == 1 )
        {
            return '';
        }

        $base_url = $_SERVER["SCRIPT_NAME"] . '?';

        foreach ( $_GET AS $chiave => $valore)
        {
            //bisogna fare attenzioni ai get da non ripetere, esempio: un get per eliminare qualcosa, non bisogna ripeterlo nell'impaginazione..
            $base_url = ($chiave != $lo) ? $base_url . $chiave . '=' . $valore . '&amp;' : $base_url;
        }
        $on_page = floor($start_item / $per_page) + 1;

        $page_string = '';
        if ( $total_pages > 10 )
        {
            $init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;

            for($i = 1; $i < $init_page_max + 1; $i++)
            {
                $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="'. $base_url .$lo. '=' . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
                if ( $i < $init_page_max )
                    $page_string .= ", ";
            }

            if ( $total_pages > 3 )
            {
                if ( $on_page > 1  && $on_page < $total_pages )
                {
                    $page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';

                    $init_page_min = ( $on_page > 4 ) ? $on_page : 5;
                    $init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;

                    for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
                    {
                        $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="'. $base_url .$lo. '=' . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
                        if ( $i <  $init_page_max + 1 )
                        {
                            $page_string .= ', ';
                        }
                    }

                    if ( $on_page != $total_pages - 1)
                        $page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
                }
                else
                    $page_string .= ' ... ';

                for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
                {
                    $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="'. $base_url .$lo. '=' . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
                    if( $i <  $total_pages )
                        $page_string .= ", ";
                }
            }
        }
        else
        {
            for($i = 1; $i < $total_pages + 1; $i++)
            {
                $page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="'. $base_url .$lo. '=' . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
                if ( $i <  $total_pages )
                    $page_string .= ', ';
            }
        }

        if ( $add_prevnext_text )
        {
            if ( $on_page > 1 )
                $page_string = ' <a href="'. $base_url .$lo. '=' . ( ( $on_page - 2 ) * $per_page ) . '"><--&nbsp;&nbsp;' . $page_string;

            if ( $on_page < $total_pages )
                $page_string .= '&nbsp;&nbsp;<a href="'. $base_url . $lo.'=' . ( $on_page * $per_page ) . '">--></a>';
        }
        return $page_string;
    }
}
?>